package com.example.mapp;

import com.example.entity.authority.PartyContract;
import com.example.mapp.buildSql.PartyContractMappSql;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2018/3/18.
 * 机构合同
 */
@Mapper
public interface PartyContractMapp {

    @Insert("insert into party_contract (" +
            "first_Party,second_Party,contract_Number,contract_Type,party_Id,remarks"+
            ",add_user_id,file_list) values (" +
            "#{firstParty},#{secondParty},#{contractNumber},#{contractType},#{partyId},#{remarks}"+
            ",#{addUserId},#{fileList})")
    @SelectKey(statement="select last_insert_id()",keyProperty = "id",keyColumn = "id",before = false,resultType = Long.class)
    int insert(PartyContract partyContract) throws Exception;

    @SelectProvider(type = PartyContractMappSql.class,method = "queryByPartyIdList")
    List<PartyContract> queryByPartyIdList(PartyContract partyContract)throws Exception;

    @Select(" select * from party_contract where party_id = #{partyId}")
    List<PartyContract> queryByPartyId(String partyId) throws Exception;

    @Delete(" delete from party_contract where id =#{id}")
    int deleteById(String id)throws Exception;

    @Select(" select * from party_contract where id = #{id}")
    PartyContract queryById(String id);

    /**
     * 更新合同
     * @param partyContract
     */
    @UpdateProvider(type = PartyContractMappSql.class,method = "updateContract")
    void updateContract(PartyContract partyContract);

    /**
     * 查询每家单位对应的合同数量
     * @return
     */
    @Select(" select p.id,sum(1) as numm  from party_contract pc LEFT JOIN party p ON pc.party_id= p.id GROUP BY p.id")
    List<Map<String, Object>> findContractList();

    /**
     * 存储项目数量
     * @return
     */

    @Select(" SELECT pyc.party_id as id, sum(1) AS numm FROM party_contract pyc LEFT JOIN party_contract_project pcp ON pyc.id = pcp.contract_id group by pyc.party_id")
    List<Map<String, Object>> findObjectList();


    /**
     * 更新合同数量
     * @param partyContract
     */
    @UpdateProvider(type = PartyContractMappSql.class,method = "updateContractNumber")
    void updateContractNumber(PartyContract partyContract);
}
